package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    //显示列表地址
    @GetMapping("/address/list")
    public ResultInfo findList() {
        //调用service
        List<Address> addressList = addressService.findList();
        //返回结果
        return ResultInfo.success(addressList);
    }
    //新增列表
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address) { // 1.接收参数
        // 2.调用service
        addressService.save(address);
        // 3.返回结果
        return ResultInfo.success();
    }

    //设置默认值
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String, Long> param) {
        //接受参数
        Long id = param.get("id");
        //调用service
        addressService.setDefault(id);
        //返回结果
        return ResultInfo.success();
    }

    // 结算(查询默认收货地址)
    @GetMapping("/address/default")
    public ResultInfo findDefault() {
        // 调用service
        Address address = addressService.findDefault();
        // 返回结果
        return ResultInfo.success(address);
    }

    // 回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        //调用service
        Address address = addressService.findById(id);
        //返回结果
        return ResultInfo.success(address);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo update(@RequestBody Address address) {
        //调用service
        addressService.update(address);
        //返回结果
        return ResultInfo.success();
    }

    @DeleteMapping("/address")
    public ResultInfo delete( Long ids) {
        //
        addressService.delete(ids);
        //
        return ResultInfo.success();
    }
}
